<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools show-before>
        <template #before>
          <span>共{{ page.total }}条记录</span>
        </template>
        <template #after>
          <el-button size="small" type="warning" @click="$router.push('/import')">excel导入</el-button>
          <el-button size="small" type="danger" @click="exportData">excel导出</el-button>
          <el-button size="small" type="primary" @click="addEmpoyee()">新增员工</el-button>
        </template>
      </page-tools>

      <!-- 放置表格和分页 -->
      <el-card>
        <el-table v-loading="loading" border :data="list">
          <el-table-column type="index" label="序号" sortable />
          <el-table-column prop="username" label="姓名" sortable />
          <el-table-column prop="mobile" label="电话号码" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" :formatter="formatEmployment" label="聘用形式" sortable>
            <!-- 格式化列表方式1,通过过滤器 -->
            <!-- <template v-slot="{row}">
              {{ row.formOfEmployment | hireType }}
            </template> -->
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" sortable />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable>
            <template v-slot="{row}">
              {{ row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column prop="enableState" align="center" label="账户状态" sortable>
            <template v-slot="{row}">
              <el-switch :value="row.enableState === 1" />
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable fixed="right" width="280">
            <template v-slot="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="deleteEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :total="page.total"
            :page-size="page.size"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
    <add-employee :show-dialog.sync="showDialog" />
  </div>
</template>

<script>
import { getEmployeesList, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees.js'
import AddEmployee from './components/add-employee.vue'
export default {
  name: 'Employees',
  filters: {
    // hireType(val) {
    //   return EmployeeEnum.hireType.find(t => t.id === val).value
    // }
  },
  components: {
    AddEmployee
  },
  props: {},
  data() {
    return {
      showDialog: false,
      list: [],
      page: {
        page: 1,
        size: 5,
        total: 0
      },
      loading: true
    }
  },
  created() {
    this.getUserList()
    this.getEmployeesList()
  },
  mounted() {},
  methods: {
    addEmpoyee() {
      this.showDialog = true
    },
    deleteEmployee(id) {
      // 通过elmecon
      // C:\Users\xxx\.vscode\extensions\ss.element-ui-snippets-0.7.2\snippets
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await delEmployee(id)
        this.getEmployeeList()
        // 通过eleme
        this.$message({
          message: '删除成功',
          type: 'success',
          showClose: true,
          duration: 3000
        })
      }).catch(() => {

      })
    },
    formatEmployment(row, column, cellValue, index) {
      const f = EmployeeEnum.hireType.find(t => t.id === cellValue)
      return f ? f.value : '未知'
    },
    async getUserList() {
      // const { rows, total } = await getUserList(this.page)
      // this.list = rows
    // this.page.total = total
    },
    handleCurrentChange(newPage) {
      this.page.page = newPage
      this.getEmployeesList()
    },
    async getEmployeesList() {
      this.loading = true
      const { rows, total } = await getEmployeesList(this.page)
      this.list = rows
      this.page.total = total
      this.loading = false
    },
    async exportData() {
      // 表头对应关系
      const headers = {
        '姓名': 'username',
        '手机号': 'mobile',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      const userHeader = Object.keys(headers)
      const { rows } = await getEmployeesList({ page: 1, size: this.page.total })
      const userList = rows.map(t => {
        return [t.username, t.mobile, t.timeOfEntry, t.formOfEmployment, t.correctionTime, t.workNumber, t.departmentName]
      })
      console.log(userList)
      // 定义复杂表头,添加的复杂表头会出现在表格的最上面
      const multiHeader = [['姓名', '主要信息', '', '', '', '', '部门']]
      // 合并单元格,: 表示从哪到哪
      const merges = ['A1:A2', 'B1:F1', 'G1:G2']
      import('@/vendor/Export2Excel').then(excel => {
        // excel 是引入文件的导出对象
        excel.export_json_to_excel({
          header: userHeader,
          data: userList,
          filename: '员工工资表',
          bookType: 'xlsx',
          multiHeader,
          merges
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
